<template>
	<view class="content">
		<view class="mineWarp">
			<view class="header" :style="{paddingTop:navBarTop + 'px',height:navBarHeight + 'px',lineHeight:navBarHeight + 'px'}">
				我的
			</view>
			
			<view class="userBox">
				<view class="userWrap" v-if="isLogout == 1">
					<view class="userImg">
						<image src="../../static/pic.png" mode=""></image>
					</view>
					
					<view class="userInfo" @click="goLogin">
						<view class="userName">
							登录/注册
						</view>
						<view class="userPhone">
							登录注册后才能看到更多内容
							<text @click.stop="changeTeacher" v-if="isMaster == 1" style="color:blue;font-size:24rpx;padding-left:30rpx;">切换师傅端</text>
						</view>
					</view>
				</view>
				
				<view class="userWrap" v-else>
					<view class="userImg">
						<image :src="userInfo.headerImage" mode=""></image>
					</view>
					<view class="userInfo">
						<view class="userName">
							{{userInfo.nickName}}<text v-if="isHide">{{bussInfo.enterpriseName}}</text>
							<label v-if="isHide"></label>
						</view>
						<view class="userPhone">
							{{userInfo.phone}}
							<text @click.stop="changeTeacher" v-if="isMaster == 1" style="color:blue;font-size:24rpx;padding-left:30rpx;">切换师傅端</text>
						</view>
					</view>
				</view>
				
				<view class="moreBtn" v-if="isLogout != 1" @click="goInformation">
					<image src="../../static/more2.png" mode=""></image>
				</view>
			</view>
			
			<view class="zichang" v-if="isLogout != 1">
				<view class="zcTxt" @click="goDeviceInfo">
					设备资产
				</view>
				
				<block v-if="isHide">
					<view class="zcAdress enterPirceName">
						{{bussInfo.enterpriseName}}({{bussInfo.address}})
					</view>
					<view class="zcNumber">
						设备总数：<text>{{deviceNumber}}台</text>(我的设备资产)
					</view>
				</block>
				
				<block v-else>
					<view v-if="isFlag" class="zcAdress" @click="goEnterPrice">
						绑定企业
					</view>
				</block>
				
				<view class="zcBtnBox">
					<view class="zcbtn" @click="scanRepair">
						扫一扫报修
					</view>
					<view class="zcbtn" @click="goDevice">
						添加设备
					</view>
				</view>
				<view class="beiImg">
					<image src="https://czjy.jorse.xyz/images/bei.png" mode=""></image>
				</view>
			</view>
			
			</view>
			
			<view class="lmwarp" style="padding:0 24rpx 24rpx;">
				<view class="lanmuList">
					<view class="lmli" @click="goOrder(0)">
						<image src="../../static/my1.png" mode=""></image>
						<text>安装服务</text>
					</view>
					<view class="lmli" @click="goOrder(1)">
						<image src="../../static/my2.png" mode=""></image>
						<text>维修服务</text>
					</view>
					<view class="lmli" @click="goOrder(2)">
						<image src="../../static/my3.png" mode=""></image>
						<text>保养换芯</text>
					</view>
					<view class="lmli" @click="goOrder(3)">
						<image src="../../static/my4.png" mode=""></image>
						<text>巡检服务</text>
					</view>
				</view>
				
				<view class="wuWarp">					
					<view class="wpli wpli1" @click="goOrderPage(1)">
						<text>增值服务订单</text>
						<view class="rightBtn"></view>
					</view>
					
					<view class="wpli wpli2" @click="goPage('/pages/mine/aboutUs')">
						<text>关于我们</text>
						<view class="rightBtn"></view>
					</view>
				</view>
				
				<view class="wuWarp">
					<view class="wpli wpli6">
						<view class="txtt">
							<button open-type="contact">联系客服</button> 
						</view>
						<view class="rightBtn"></view>
					</view>
					
					<view class="wpli wpli4" @click="goOrderPage(2)">
						<text>加入我们</text>
						<view class="rightBtn"></view>
					</view>
					
					<view class="wpli wpli5" @click="goRepair">
						<text>我要报修</text>
						<view class="rightBtn"></view>
					</view>
				</view>
			</view>
		
		<bottomPage :suo="2"></bottomPage>
		
		<u-modal :show="show" width="650rpx" title="获取用户信息" @confirm="confirmUser"
			:showCancelButton="true" @cancel="show = false">
			<view class="slot-content" style="width:100%;">
				<view class="informationBox">
					<view class="myli">
						<label>头像</label>
						<view class="rightInfo">
							<button class="uuButton" v-if="loginInfo.headerImage == ''" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
								获取头像
							</button>
							<image v-else :src="loginInfo.headerImage" mode=""></image>
						</view>
					</view>
					
					<view class="myli">
						<label>昵称</label>
						<view class="rightInfo">
							<input type="nickname" style="text-align: right;" placeholder="获取昵称" 
								v-model="loginInfo.nickName" @blur="onBlur" />
						</view>
					</view> 
					<view class="myli">
						<label>手机号码</label>
						<view class="rightInfo">
							<view class="">
								<u-button v-if="loginInfo.phone == ''" class="uuButton" open-type="getPhoneNumber" @getphonenumber="getNumber">
									获取手机号
								</u-button>
								<block v-else>
									{{loginInfo.phone}}
								</block>
							</view>
							
						</view>
					</view>
				</view>
			
			</view>
		</u-modal>
		
	</view>
</template>

<script>
	import bottomPage from '@/components/bottom.vue';
	import { getCooperationDetail, getUserInfoDetail, loginForm, setUserInfo, updateUserInfo } from '@/api/login.js';
	import baseUrl  from '@/util/config.js';
	export default {
		components:{
			bottomPage
		},
		data() {
			return {
				isBind: false,
				show: false,
				isLogin: false,
				isFlag: true,
				navBarTop: null,
				navBarHeight: null,
				isHide: true,
				deviceNumber: 0,
				isLogout: '',
				isMaster: '',
				userInfo:{},
				bussInfo:{},
				loginInfo:{
					nickName: '',
					headerImage: '',
					phone: ''
				},
			}
		},
		onLoad() {
			this.isLogout = uni.getStorageSync('isLogout');
			let statuBar = uni.getSystemInfoSync().statusBarHeight;
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			this.navBarTop = menuButtonInfo.top;
			this.navBarHeight = menuButtonInfo.height;	 
		},
		onShow() {
			this.initForm();
			console.log(12341234);
		},
		onPullDownRefresh() {
			this.initForm();
		},
		methods: {
			onChooseAvatar(e){
				uni.uploadFile({
					url: baseUrl.baseUrl + '/common/upload',
					filePath: e.detail.avatarUrl,
					name: 'file',
					header: {
						Authorization: 'Bearer ' + uni.getStorageSync('userToken')
					},
					formData: {
						frontFileType: 'picture'
					},
					success: res => {
						var jsurl = JSON.parse(res.data)
						if (jsurl.code != 200) {
							return uni.$u.toast(jsurl.msg);
						}
						this.loginInfo.headerImage = jsurl.url;
					}
				});	
			},
			// 确定功能
			confirmUser(){
				if(this.loginInfo.headerImage == '') {
					return uni.$u.toast('请选择用户头像!');
				}
				if(this.loginInfo.nickName == '') {
					return uni.$u.toast('请选择用户昵称!');
				}
				if(this.loginInfo.phone == '') {
					return uni.$u.toast('请获取手机号!');
				}
				this.loginInfo.id = uni.getStorageSync('userInfo').id;
				updateUserInfo(this.loginInfo).then(res=>{
					if(res.code == 200) {
						getUserInfoDetail({id: uni.getStorageSync('userInfo').id}).then(resbb=>{
							if(resbb.code == 200) {
								uni.setStorageSync('userInfo',resbb.data);
								this.isLogout = uni.getStorageSync('isLogout');
								this.show = false;
								if(resbb.data.isMaster == 1) {
									uni.reLaunch({
										url: '/pages/engineer/index'
									});
								} else {
									this.initForm();
								}
							} else {
								uni.$u.toast(resbb.msg);
							}
						});
					} else {
						uni.$u.toast(res.msg);
					}
				});
			},
			// 手机号登录
			getNumber(e) {
				const pmm = {
					openId: uni.getStorageSync('openId'),
					phoneCode: e.detail.code,
					agentId: 1
				};
				setUserInfo(pmm).then(res=>{
					if(res.code == 200) {
						this.loginInfo.phone = res.data.wxUser.phone;
					} else {
						uni.$u.toast(res.msg);
					}	
				})
			},
			goOrderPage(type){
				if(uni.getStorageSync('userToken') == '') {
					return uni.$u.toast('请先登录！');
				}
				if(type == 1) {
					uni.navigateTo({
						url: '/pages/increment/orderList'
					})
				} else if(type == 2) {
					uni.navigateTo({
						url: '/pages/mine/joinUs'
					});
				}
				
			},
			// 登录功能
			goLogin(){
				uni.login({
					provider: 'weixin', //使用微信登录
					success:ress=> {
						console.log(ress);
						loginForm({code:ress.code}).then(res=>{
							uni.setStorageSync('userToken',res.data.token);
							uni.setStorageSync('needReg',res.data.needReg);
							uni.setStorageSync('openId',res.data.openId);
							uni.setStorageSync('userInfo',res.data.wxUser);
							uni.setStorageSync('isLogout',0);
							this.show = true;
						});
					}
				});
			},
			changeTeacher(){
				console.log(1234)
				let userInfo = uni.getStorageSync('userInfo');
				userInfo.isMaster = 1;
				uni.setStorageSync('isChange',1);
				uni.setStorageSync('userInfo',userInfo);
				console.log(userInfo,'===1234')
				uni.reLaunch({
					url: '/pages/index/index'
				});
			},
			initForm(){
				if(uni.getStorageSync('userToken') == '') {
					this.isLogout = 1;
					return;
				}	
				getUserInfoDetail({id: uni.getStorageSync('userInfo').id}).then(res=>{
					this.isMaster = res.data.isMaster == null?'':res.data.isMaster;
					console.log(this.isMaster,'===1234');
					uni.stopPullDownRefresh();
					if(res.code == 200) {
						console.log(res.data);
						if(uni.getStorageSync('isChange') != 1) {
							uni.setStorageSync('userInfo',res.data);
						}
						console.log(uni.getStorageSync('userInfo'),'===1234');
						if(res.data.companyId != null) {
							this.isBind = true;
							if(res.data.companyName == null) {
								this.isFlag = false;
							} else {
								this.isFlag = true;
							}
							this.isLogin = res.data.companyName == null;
							this.userInfo = res.data;
							this.deviceNumber = res.data.deviceNumber;
							this.isHide = true;
							this.getEnterPriceInfo(res.data.companyId);
						} else {
							console.log('进来啦====')
							this.isBind = false;
							this.$set(this.userInfo,'headerImage',res.data.headerImage);
							this.$set(this.userInfo,'nickName',res.data.nickName);
							this.$set(this.userInfo,'phone',res.data.phone);
							console.log(this.userInfo,'=======1234');
							console.log(res.data,'------44444')
							this.isHide = false;
						}
					} else{
						this.isHide = false;
						this.isBind = false;
						this.isLogin = false;
						uni.$u.toast(res.msg);
					}
					
				});	
			},
			// 跳转到报修页面
			goRepair(){
				if(uni.getStorageSync('userToken') == '') {
					return uni.$u.toast('请先登录！');
				}
				uni.navigateTo({
					url: '/pages/index/service?idd=0'
				});
				return;
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }	
				if(this.isBind == false) {
					uni.navigateTo({
						url: '/pages/mine/joinUs?type=1'
					});
				} else {
					if(this.isLogin === true) {
					  return uni.$u.toast('请等待审核！');
					} else {
						uni.navigateTo({
							url: '/pages/index/service?idd=0'
						});
					}
				}
			},
			commonCheck() {
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				if(this.isBind == false) {
					uni.navigateTo({
						url: '/pages/mine/joinUs?type=1'
					});
				} else {
					if(this.isLogin === true) {
					   uni.$u.toast('请等待审核！');
					   return false;
					}
				}
			},
			scanRepair(){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				if(this.isBind == false) {
					uni.navigateTo({
						url: '/pages/mine/joinUs?type=1'
					});
				} else {
					if(this.isLogin === true) {
					  return uni.$u.toast('请等待审核！');
					} else {
						uni.scanCode({
							scanType: ["qrCode"],
							success: (res) => {
								console.log(res);
								if (res.result) {
									uni.navigateTo({
										url: '/pages/index/service?idd=0&deviceCode=' + res.result
									});
								} else {
									uni.$u.toast('请重新扫描!');
									return false;
								}
							}
						});
					}
				}
					
			},
			goEnterPrice(){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				uni.navigateTo({
					url: '/pages/mine/joinUs?type=1'
				})
			},
			getEnterPriceInfo(idd){
				getCooperationDetail({id: idd}).then(res=>{
					if(res.code == 200) {
						this.isHide = true;
						this.bussInfo = res.data;
					} else {
						this.isHide = false;
						uni.$u.toast(res.msg);
					}
				});
			},
			goDeviceInfo(){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				uni.navigateTo({
					url: '/pages/mine/deviceList'
				});	
				return;
				if(this.isBind == false) {
					uni.navigateTo({
						url: '/pages/mine/joinUs?type=1'
					});
				} else {
					if(this.isLogin === true) {
					  return uni.$u.toast('请等待审核！');
					} else {
						uni.navigateTo({
							url: '/pages/mine/deviceList'
						});	
					}
				}
				
			},
			goInformation(){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				uni.navigateTo({
					url: '/pages/mine/information'
				})
			},
			goDevice(){
				if(uni.getStorageSync('userToken') == '') {
					return uni.$u.toast('请先登录！');
				}
				uni.navigateTo({
					url: '/pages/index/addDevice'
				})
				return;
				if(this.isBind == false) {
					uni.navigateTo({
						url: '/pages/mine/joinUs?type=1'
					});
				} else {
					if(this.isLogin === true) {
					  return uni.$u.toast('请等待审核！');
					} else {
						uni.navigateTo({
							url: '/pages/index/addDevice'
						})
					}
				}
				
			},
			goOrder(type){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				if(uni.getStorageSync('userToken') == '') {
					return uni.$u.toast('请先登录！');
				}	
				// uni.navigateTo({
				// 	url: '/pages/index/service?idd=' + type
				// })
				uni.navigateTo({
					url: '/pages/order/index?idd=' + type
				});
				return;
				if(this.isBind == false) {
					// uni.navigateTo({
					// 	url: '/pages/mine/joinUs?type=1'
					// });
					uni.navigateTo({
						url: '/pages/index/service?idd=' + type
					})
				} else {
					if(this.isLogin === true) {
					  return uni.$u.toast('请等待审核！');
					} else {
						uni.navigateTo({
							url: '/pages/order/index?idd=' + type
						});
					}
				}
			},
			goPage(page){
				// if(uni.getStorageSync('isLogout') == 1) {
				// 	  return uni.$u.toast('请先登录！');
				// }
				uni.navigateTo({
					url: page
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.content{
	background-color:#F4F5F7;padding-bottom:180rpx;
	.informationBox{
		background-color:#fff;border-radius:12rpx;box-shadow: 0px -1px 0px 0px #F0F0F0 inset;
		.myli{
			@include flx;justify-content: space-between;padding:0 32rpx;border-bottom:1px solid #F0F0F0;
			label{
				display: block;@include baseFont(30rpx,#666,112rpx);width:25%;
			}
			.rightInfo{
				@include baseFont(30rpx,#333,112rpx);@include flx;align-items: center;width:75%;justify-content: flex-end;
				image{
					display: block;width:88rpx;height:88rpx;border-radius:50%;
				}
				button{
					color:#665CF6!important;margin:0!important;
				}
				input{
					text-align: right;
				}
				.rightImg{
					width:24rpx;height:26rpx;background: url('../../static/nameRight.png') no-repeat;background-size:24rpx 26rpx;
					margin-left:12rpx;
				}
			}
		}
	}
	.mineWarp{
		background: linear-gradient( 180deg, #BED4FF 0%, #F4F5F7 100%);padding:0 24rpx;
		// height:606rpx;
		.header{
			text-align: center;margin-bottom:26rpx;
		}
			.userBox{
				margin-bottom: 32rpx;position: relative;@include flx;justify-content: space-between;align-items: center;
				.moreBtn{
					image{
						width:40rpx;height:40rpx;
					}
				}
				.userWrap{
					@include flx;
					.userImg{
						margin-right:26rpx;
						image{
							display: block;width:108rpx;height:108rpx;border-radius:50%;border:1px solid #fff;
						}
					}
					.userInfo{ 
						.userName{
							@include baseFont(36rpx,rgba(0, 0, 0, 0.85),42rpx);font-weight:500;margin-bottom:12rpx;
							@include flx;
							text{
								display: block;height:40rpx;border: 0.8px solid rgba(54, 113, 233, 0.1);border-radius:6rpx;
								background-color:#fff;text-align: center;font-size:22rpx;line-height:40rpx;color:#141414;
								margin:0 22rpx 0 12rpx;padding:0 16rpx;
							}
							label{
								display: block;width:32rpx;height:32rpx;background: url('../../static/vip.png') no-repeat;
								background-size:32rpx;
							}
						}
						.userPhone{
							@include baseFont(28rpx,#7D7D7D,42rpx);padding-left:44rpx;background:url('../../static/phone.png') no-repeat left center;
							background-size:36rpx;
						}
					}
				}
			}
		}
		.zichang{
			background: linear-gradient(89.83deg, #3671E9 -25.22%, #1B3F87 101.47%);height:268rpx;border-radius:16rpx;
			margin-bottom:16rpx;position: relative;box-shadow: 0px 8rpx 8rpx 0px rgba(0,0,0,0.01);
			.zcTxt{
				width:182rpx;height:52rpx;background: url('../../static/ledge.png') no-repeat;background-size:182rpx 52rpx;
				@include baseFont(26rpx,#fff,52rpx);font-weight:500;text-align: center;
			}
			.beiImg{
				image{
					width:194rpx;height:194rpx;position: absolute;right:20rpx;top:48rpx;
				}
			}
			.zcAdress{
				@include baseFont(30rpx,#fff,40rpx);margin:16rpx 0 8rpx 28rpx;
			}
			.enterPirceName{
			  width: 500rpx; /* 限制文本宽度 */
			  overflow: hidden; /* 隐藏溢出的内容 */
			  white-space: nowrap; /* 防止文本换行 */
			  text-overflow: ellipsis; /* 超出部分显示为省略号 */
			}
			.zcNumber{
				@include baseFont(26rpx,#fff,40rpx);margin:0 0 0 28rpx;
				text{
					font-size:28rpx;font-weight:500;
				}
			}
			.zcBtnBox{
				@include flx;margin-left:28rpx;margin-top:20rpx;
				.zcbtn{
					width:154rpx;height:60rpx;background:#E5EEFF url('../../static/can1.png') no-repeat left 24rpx center;
					border-radius:32rpx;text-align: left;background-size:32rpx;
					@include baseFont(26rpx,#3671E9,60rpx);margin-right:16rpx;padding-left:76rpx;
				}
				.zcbtn:last-of-type{
					background:#E5EEFF url('../../static/can2.png') no-repeat left 24rpx center;background-size:32rpx;
				}
			}
		}
		.wuWarp{
			border-radius:16rpx;background-color:#fff;margin-bottom:16rpx;
			.wpli{
				padding:0 92rpx;position: relative;
				text,.txtt{
					display: block;@include baseFont(30rpx,#333,104rpx);border-bottom:1px solid rgba(0, 0, 0, 0.08);
				}
				.rightBtn{
					width:34rpx;height:32rpx;background: url('../../static/mineRight.png') no-repeat;
					position: absolute;top:50%;right:28rpx;transform: translateY(-50%);background-size: 34rpx 32rpx;
				}
			}
			.wpli:last-of-type{
				text{
					border-bottom:none;
				}
			}
			.wpli1{
				background: url('../../static/my5.png') no-repeat left 32rpx center;background-size:40rpx;
			}
			.wpli2{
				background: url('../../static/my6.png') no-repeat left 32rpx center;background-size:40rpx;border-bottom: none!important;
			}
			.wpli3{
				background: url('../../static/my5.png') no-repeat left 32rpx center;background-size:40rpx;
			}
			.wpli4{
				background: url('../../static/my7.png') no-repeat left 32rpx center;background-size:40rpx;
			}
			.wpli5{
				background: url('../../static/my8.png') no-repeat left 32rpx center;background-size:40rpx;border-bottom: none!important;
			}
			.wpli6{
				background: url('../../static/my9.png') no-repeat left 32rpx center;background-size:40rpx;border-bottom: none!important;
			}
		}
		.lanmuList{
			border-radius:16rpx;background-color:#fff;@include flx;padding:30rpx 56rpx 26rpx 30rpx;justify-content: space-between;
			margin-bottom:16rpx;
			.lmli{
				image{
					display: block;width:66rpx;height:66rpx;margin:0 auto;
				}
				text{
					display: block;margin-top:12rpx;@include baseFont(28rpx,#333,40rpx);
				}
			}
		}
	
}
</style>
